<?php
ob_start(); session_start();

class MemberController extends Controller {
    
    public function actionCheckLogin() {
        $attributes = array();
        $attributes["member_username"] = $_POST["user_username"];
        $attributes["member_password"] = md5($_POST["user_password"]);
//        $attributes["member_password"] = $_POST["user_password"];
        $attributes["member_status"] = 1;
        $attributes["member_level"] = "admin";
        
        $model = Member::model()->findByAttributes($attributes);
    
        if (!empty($model)) {
            Yii::app()->session["member_id"] = $model->id;
            Yii::app()->session["member_username"] = $model->member_username;
            Yii::app()->session["member_level"] = $model->member_level;
            
//            $this->redirect(array("home"));
            $this->redirect(array("home/index"));
        } else {
            $this->redirect(Yii::app()->homeUrl);
        }
    }
    
    public function actionHome() {
        $this->render("home");
    }
    
    public function actionLogout() {
        unset(Yii::app()->session["member_id"]);
        unset(Yii::app()->session["member_username"]);
        unset(Yii::app()->session["member_level"]);
        
        $this->redirect(Yii::app()->homeUrl);
    }
    
    public function actionMemberRegister($id = null) {
        $model = new Member();
        
        // Save
        if (!empty($_POST["Member"])) {
            if (!empty($_POST["Member"]["id"])) {
                $id = $_POST["Member"]["id"];
                $model = Member::model()->findByPk($id);
            }
            $model->_attributes = $_POST["Member"];
            $model->member_password = md5($_POST["Member"]["member_password"]);
            
            if ($model->save()) {
                $this->redirect(array("Setting/Member"));
            }
        }
        
        // Render
        if (!empty($id)) {
            $model = Member::model()->findByPk($id);
        }
        
        $this->render("MemberRegister", array(
            "model" => $model
        ));
    }
    
}
